use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.

my $uname=`uname`;
my $params=undef;

if ( $uname =~ /SunOS/i ) {
	# $params->{OPTIMIZE} = " ";
	# $params->{CCCDLFLAGS} = " -L$ENV{OPENSSL_PREFIX}/lib ";
	# $params->{OTHERLDFLAGS} = "-R${OPENSSL_PREFIX}/lib";
}

$params->{NAME} = "OpenCA::OpenSSL";
$params->{VERSION_FROM} = "OpenSSL.pm";
$params->{LIBS} = ["-L$ENV{OPENSSL_PREFIX}/lib -lcrypto -lssl"];
$params->{LDFLAGS} = "-L$ENV{OPENSSL_PREFIX}/lib -lcrypto -lssl";
$params->{PREREQ_PM} = {'X500::DN' => '0.27'};
$params->{INC} = "-I. -I$ENV{OPENSSL_PREFIX}/include";

# $params->{EXTRALIBS} = "-L$ENV{OPENSSL_PREFIX}/lib -lcrypto -lssl";
# $params->{LDLOADLIBS} = "-L$ENV{OPENSSL_PREFIX}/lib -lcrypto -lssl";
# $params->{LD_RUN_PATH} = "$ENV{OPENSSL_PREFIX}/lib";

WriteMakefile ( %{ $params } );

# WriteMakefile(
#     'NAME'		=> 'OpenCA::OpenSSL',
#     'VERSION_FROM'	=> 'OpenSSL.pm', # finds $VERSION
#     'LIBS'		=> ["-L$ENV{OPENSSL_PREFIX}/lib -lcrypto -lssl"],
#     'PREREQ_PM'		=> {'X500::DN' => '0.27'},
#     'INC'		=> "-I. -I$ENV{OPENSSL_PREFIX}/include"
# );

if  (eval {require ExtUtils::Constant; 1}) {
  # If you edit these definitions to change the constants used by this module,
  # you will need to use the generated const-c.inc and const-xs.inc
  # files to replace their "fallback" counterparts before distributing your
  # changes.
  my @names = (qw(CTX_TEST EXFLAG_BCONS EXFLAG_CA EXFLAG_INVALID EXFLAG_KUSAGE
		 EXFLAG_NSCERT EXFLAG_SET EXFLAG_SS EXFLAG_V1 EXFLAG_XKUSAGE
		 GEN_DIRNAME GEN_DNS GEN_EDIPARTY GEN_EMAIL GEN_IPADD
		 GEN_OTHERNAME GEN_RID GEN_URI GEN_X400 KU_CRL_SIGN
		 KU_DATA_ENCIPHERMENT KU_DECIPHER_ONLY KU_DIGITAL_SIGNATURE
		 KU_ENCIPHER_ONLY KU_KEY_AGREEMENT KU_KEY_CERT_SIGN
		 KU_KEY_ENCIPHERMENT KU_NON_REPUDIATION NS_OBJSIGN
		 NS_OBJSIGN_CA NS_SMIME NS_SMIME_CA NS_SSL_CA NS_SSL_CLIENT
		 NS_SSL_SERVER X509V3_EXT_CTX_DEP X509V3_EXT_DYNAMIC
		 X509V3_EXT_MULTILINE X509V3_F_COPY_EMAIL X509V3_F_COPY_ISSUER
		 X509V3_F_DO_EXT_CONF X509V3_F_DO_EXT_I2D
		 X509V3_F_HEX_TO_STRING X509V3_F_I2S_ASN1_ENUMERATED
		 X509V3_F_I2S_ASN1_INTEGER X509V3_F_I2V_AUTHORITY_INFO_ACCESS
		 X509V3_F_NOTICE_SECTION X509V3_F_NREF_NOS
		 X509V3_F_POLICY_SECTION X509V3_F_R2I_CERTPOL
		 X509V3_F_S2I_ASN1_IA5STRING X509V3_F_S2I_ASN1_INTEGER
		 X509V3_F_S2I_ASN1_OCTET_STRING X509V3_F_S2I_ASN1_SKEY_ID
		 X509V3_F_S2I_S2I_SKEY_ID X509V3_F_STRING_TO_HEX
		 X509V3_F_SXNET_ADD_ASC X509V3_F_SXNET_ADD_ID_INTEGER
		 X509V3_F_SXNET_ADD_ID_ULONG X509V3_F_SXNET_GET_ID_ASC
		 X509V3_F_SXNET_GET_ID_ULONG X509V3_F_V2I_ACCESS_DESCRIPTION
		 X509V3_F_V2I_ASN1_BIT_STRING X509V3_F_V2I_AUTHORITY_KEYID
		 X509V3_F_V2I_BASIC_CONSTRAINTS X509V3_F_V2I_CRLD
		 X509V3_F_V2I_EXT_KU X509V3_F_V2I_GENERAL_NAME
		 X509V3_F_V2I_GENERAL_NAMES X509V3_F_V3_GENERIC_EXTENSION
		 X509V3_F_X509V3_ADD_VALUE X509V3_F_X509V3_EXT_ADD
		 X509V3_F_X509V3_EXT_ADD_ALIAS X509V3_F_X509V3_EXT_CONF
		 X509V3_F_X509V3_EXT_I2D X509V3_F_X509V3_GET_VALUE_BOOL
		 X509V3_F_X509V3_PARSE_LIST X509V3_F_X509_PURPOSE_ADD
		 X509V3_R_BAD_IP_ADDRESS X509V3_R_BAD_OBJECT
		 X509V3_R_BN_DEC2BN_ERROR X509V3_R_BN_TO_ASN1_INTEGER_ERROR
		 X509V3_R_DUPLICATE_ZONE_ID X509V3_R_ERROR_CONVERTING_ZONE
		 X509V3_R_ERROR_IN_EXTENSION X509V3_R_EXPECTED_A_SECTION_NAME
		 X509V3_R_EXTENSION_NAME_ERROR X509V3_R_EXTENSION_NOT_FOUND
		 X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED
		 X509V3_R_EXTENSION_VALUE_ERROR X509V3_R_ILLEGAL_HEX_DIGIT
		 X509V3_R_INVALID_BOOLEAN_STRING
		 X509V3_R_INVALID_EXTENSION_STRING X509V3_R_INVALID_NAME
		 X509V3_R_INVALID_NULL_ARGUMENT X509V3_R_INVALID_NULL_NAME
		 X509V3_R_INVALID_NULL_VALUE X509V3_R_INVALID_NUMBER
		 X509V3_R_INVALID_NUMBERS X509V3_R_INVALID_OBJECT_IDENTIFIER
		 X509V3_R_INVALID_OPTION X509V3_R_INVALID_POLICY_IDENTIFIER
		 X509V3_R_INVALID_SECTION X509V3_R_INVALID_SYNTAX
		 X509V3_R_ISSUER_DECODE_ERROR X509V3_R_MISSING_VALUE
		 X509V3_R_NEED_ORGANIZATION_AND_NUMBERS
		 X509V3_R_NO_CONFIG_DATABASE X509V3_R_NO_ISSUER_CERTIFICATE
		 X509V3_R_NO_ISSUER_DETAILS X509V3_R_NO_POLICY_IDENTIFIER
		 X509V3_R_NO_PUBLIC_KEY X509V3_R_NO_SUBJECT_DETAILS
		 X509V3_R_ODD_NUMBER_OF_DIGITS
		 X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS
		 X509V3_R_UNABLE_TO_GET_ISSUER_KEYID
		 X509V3_R_UNKNOWN_BIT_STRING_ARGUMENT
		 X509V3_R_UNKNOWN_EXTENSION X509V3_R_UNKNOWN_EXTENSION_NAME
		 X509V3_R_UNKNOWN_OPTION X509V3_R_UNSUPPORTED_OPTION
		 X509V3_R_USER_TOO_LONG X509_PURPOSE_ANY X509_PURPOSE_CRL_SIGN
		 X509_PURPOSE_DYNAMIC X509_PURPOSE_DYNAMIC_NAME
		 X509_PURPOSE_MAX X509_PURPOSE_MIN X509_PURPOSE_NS_SSL_SERVER
		 X509_PURPOSE_SMIME_ENCRYPT X509_PURPOSE_SMIME_SIGN
		 X509_PURPOSE_SSL_CLIENT X509_PURPOSE_SSL_SERVER XKU_CODE_SIGN
		 XKU_SGC XKU_SMIME XKU_SSL_CLIENT XKU_SSL_SERVER));
  ExtUtils::Constant::WriteConstants(
                                     NAME         => 'OpenCA::OpenSSL::Fast',
                                     NAMES        => \@names,
                                     DEFAULT_TYPE => 'IV',
                                     C_FILE       => 'const-c.inc',
                                     XS_FILE      => 'const-xs.inc',
                                  );

}
else {
  use File::Copy;
  use File::Spec;
  foreach my $file ('const-c.inc', 'const-xs.inc') {
    my $fallback = File::Spec->catfile('fallback', $file);
    copy ($fallback, $file) or die "Can't copy $fallback to $file: $!";
  }
}
